/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		   */
/*																   */
/*	NAME	  = t__sqlserver_bigint_add.c						   */
/*	NOTE	  = 												   */
/*	DATE	  = 2014/11/25 by geyh								   */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include <CUnit/Basic.h>

#include <postgresql/config.h>
#include <postgresql/postgresql_core.h>
#include <postgresql/postgresql_comm.h>

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __postgresql_parse_err_resp(Frbyte_Packet *p_st_data, int i_packet_len, long *p_l_err_code,
                                       char **pp_err_msg);

static void __case1(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 88;
    Frbyte_Packet *p_st_data = NULL;
    long l_err_code = 0;
    char *p_err_msg = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,
                 (char *) "\x53\xe9\x94\x99\xe8\xaf\xaf\x00\x43\x34\x32\x36\x30\x31\x00\x4d\xe8\xaf\xad\xe6\xb3\x95\xe9\x94\x99\xe8\xaf\xaf\x20\xe5\x9c\xa8\x20\x22\x65\x78\x69\x74\x22\x20\xe6\x88\x96\xe9\x99\x84\xe8\xbf\x91\xe7\x9a\x84\x00\x50\x31\x00\x46\x73\x63\x61\x6e\x2e\x6c\x00\x4c\x31\x30\x30\x35\x00\x52\x73\x63\x61\x6e\x6e\x65\x72\x5f\x79\x79\x65\x72\x72\x6f\x72\x00\x00",
                 88);

    postgresql_quicksort(0, -1);
    i_ret = __postgresql_parse_err_resp(p_st_data, i_packet_len, &l_err_code, &p_err_msg);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(l_err_code, 42601);
    //CU_ASSERT_PTR_NULL(p_err_msg);
    CU_ASSERT_STRING_EQUAL(p_err_msg,
                           (char *) "\xe8\xaf\xad\xe6\xb3\x95\xe9\x94\x99\xe8\xaf\xaf\x20\xe5\x9c\xa8\x20\x22\x65\x78\x69\x74\x22\x20\xe6\x88\x96\xe9\x99\x84\xe8\xbf\x91\xe7\x9a\x84");

    frfree(p_err_msg);
    bytebuff_free(&p_st_data);

    return;
}

static void __case2(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 87;
    Frbyte_Packet *p_st_data = NULL;
    long l_err_code = 0;
    char *p_err_msg = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,
                 (char *) "\x53\xe9\x94\x99\xe8\xaf\xaf\x00\x43\x34\x32\x36\x30\x31\x00\x4d\xe8\xaf\xad\xe6\xb3\x95\xe9\x94\x99\xe8\xaf\xaf\x20\xe5\x9c\xa8\x20\x22\x65\x78\x69\x74\x22\x20\xe6\x88\x96\xe9\x99\x84\xe8\xbf\x91\xe7\x9a\x84\x00\x50\x31\x00\x46\x73\x63\x61\x6e\x2e\x6c\x00\x4c\x31\x30\x30\x35\x00\x52\x73\x63\x61\x6e\x6e\x65\x72\x5f\x79\x79\x65\x72\x72\x6f\x72\x00",
                 87);

    postgresql_quicksort(0, -1);
    i_ret = __postgresql_parse_err_resp(p_st_data, i_packet_len, &l_err_code, &p_err_msg);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);

    return;
}

static void __case3(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 88;
    Frbyte_Packet *p_st_data = NULL;
    long l_err_code = 0;
    char *p_err_msg = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,
                 (char *) "\x53\xe9\x94\x99\xe8\xaf\xaf\x00\x43\x34\x32\x36\x30\x31\x00\x4d\xe8\xaf\xad\xe6\xb3\x95\xe9\x94\x99\xe8\xaf\xaf\x20\xe5\x9c\xa8\x20\x22\x65\x78\x69\x74\x22\x20\xe6\x88\x96\xe9\x99\x84\xe8\xbf\x91\xe7\x9a\x84\x00\x50\x31\x00\x46\x73\x63\x61\x6e\x2e\x6c\x00\x4c\x31\x30\x30\x35\x00\x52\x73\x63\x61\x6e\x6e\x65\x72\x5f\x79\x79\x65\x72\x72\x6f\x72\x00\x01",
                 88);

    postgresql_quicksort(0, -1);
    i_ret = __postgresql_parse_err_resp(p_st_data, i_packet_len, &l_err_code, &p_err_msg);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);

    return;
}

static void __case4(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 88;
    Frbyte_Packet *p_st_data = NULL;
    long l_err_code = 0;
    char *p_err_msg = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,
                 (char *) "\x53\xe9\x94\x99\xe8\xaf\xaf\x00\x44\x34\x32\x36\x30\x31\x00\x4d\xe8\xaf\xad\xe6\xb3\x95\xe9\x94\x99\xe8\xaf\xaf\x20\xe5\x9c\xa8\x20\x22\x65\x78\x69\x74\x22\x20\xe6\x88\x96\xe9\x99\x84\xe8\xbf\x91\xe7\x9a\x84\x00\x50\x31\x00\x46\x73\x63\x61\x6e\x2e\x6c\x00\x4c\x31\x30\x30\x35\x00\x52\x73\x63\x61\x6e\x6e\x65\x72\x5f\x79\x79\x65\x72\x72\x6f\x72\x00\x00",
                 88);

    postgresql_quicksort(0, -1);
    i_ret = __postgresql_parse_err_resp(p_st_data, i_packet_len, &l_err_code, &p_err_msg);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(l_err_code, 0);
    //CU_ASSERT_PTR_NULL(p_err_msg);
    CU_ASSERT_STRING_EQUAL(p_err_msg,
                           (char *) "\xe8\xaf\xad\xe6\xb3\x95\xe9\x94\x99\xe8\xaf\xaf\x20\xe5\x9c\xa8\x20\x22\x65\x78\x69\x74\x22\x20\xe6\x88\x96\xe9\x99\x84\xe8\xbf\x91\xe7\x9a\x84");

    frfree(p_err_msg);
    bytebuff_free(&p_st_data);

    return;
}

static void __case5(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 88;
    Frbyte_Packet *p_st_data = NULL;
    long l_err_code = 0;
    char *p_err_msg = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data,
                 (char *) "\x53\xe9\x94\x99\xe8\xaf\xaf\x00\x43\x34\x32\x36\x30\x31\x00\x4d\xe8\xaf\xad\xe6\xb3\x95\xe9\x94\x99\xe8\xaf\xaf\x20\xe5\x9c\xa8\x20\x22\x65\x78\x69\x74\x22\x20\xe6\x88\x96\xe9\x99\x84\xe8\xbf\x91\xe7\x9a\x84\x00\x50\x31\x00\x46\x73\x63\x61\x6e\x2e\x6c\x00\x4c\x31\x30\x30\x35\x00\x4d\x73\x63\x61\x6e\x6e\x65\x72\x5f\x79\x79\x65\x72\x72\x6f\x72\x00\x00",
                 88);

    postgresql_quicksort(0, -1);
    i_ret = __postgresql_parse_err_resp(p_st_data, i_packet_len, &l_err_code, &p_err_msg);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);

    return;
}

int add_suite__postgresql_parse_err_resp(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __postgresql_parse_err_resp()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
